﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DA;
using BL;
using OrderDesk.Controllers;
using BaseLib.Localiztion;
using BL.Membership;

namespace OrderDesk.Areas.Admin.Controllers
{
    [AuthorizePage(UserPermission.AllClientsAdmin)]
    public class ClientsController : BaseController
    {
        private ClientsRepository rep = new ClientsRepository();

        //
        // GET: /Admin/Categories/

        public ViewResult Index()
        {
            var clients = rep.GetEntitySet();
            return View(clients.ToList());
        }

        private IEnumerable<SelectListItem> GetPerms(Client client)
        {
            Type userPermissionType = typeof(UserPermission);

            var permList =
                Enum.GetValues(userPermissionType)
                .Cast<UserPermission>()
                .Where(x => x != UserPermission.None)
                .Select(x =>
                    new SelectListItem()
                    {
                        Value = ((long)x).ToString(),
                        Text = x.ToString(),
                        Selected = client != null ? client.GetPermissions().HasFlag(x) : false
                    });

            return permList;
        }

        public ActionResult Edit(int? id)
        {
            Client client = null;
            if (id.HasValue)
                client = rep.GetEntity(id.Value);

            ViewBag.PermList = GetPerms(client);

            return View(client);
        }


        [HttpPost]
        public ActionResult Edit(int? id, Client model, FormCollection col)
        {
            Client client;
            if (id.HasValue)
                client = rep.GetEntity(id.Value);
            else
                client = model;

            try
            {
                var selectedPerms = col.AllKeys
                            .Where(x => x.StartsWith("perm_") && col[x].ToLower().Contains("true"))
                            .Select(x => (UserPermission)Enum.Parse(typeof(UserPermission), x.Substring("perm_".Length)));

                var perm = UserPermission.None;
                foreach (var currPerm in selectedPerms)
                {
                    perm |= currPerm;
                }

                client.SetPermissions(perm);
            }
            catch
            {
                ModelState.AddModelError("badPerms", Translator.Translate("Client_err_bad_perms"));
            }

            if (ModelState.IsValid)
            {
                if (id.HasValue)
                    client.Name = model.Name;

                rep.Save(client);
                return RedirectToAction("Index");
            }

            ViewBag.PermList = GetPerms(client);

            return View(model);
        }

        //
        // GET: /Admin/Categories/Delete/5

        public ActionResult Delete(int id)
        {
            Client client = rep.GetEntity(id);
            return View(client);
        }

        //
        // POST: /Admin/Categories/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Client client = rep.GetEntity(id);
            rep.Delete(client);

            return RedirectToAction("Index");
        }
    }
}